package com.pirestupppai.imagetool.virtualtool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.pirestupppai.common.Constant;
import com.pirestupppai.utils.Utils;

/**
 *  图片虚化View
 */

public class VirtualView extends View {
    // Alpha取值范围为0~255，数值越小越透明。
    private Context mContext;
    private Paint mVirtualPaint;
    private Bitmap mBackgroundBitmap;
    private int surplusHeight;              // 当前屏幕剩余高度
    private float scaleRate;                // 图片缩放比例
    private Matrix mBackgroundMatrix;
    private int mAlpha = 127;

    public VirtualView(Context context) {
        this(context, null);
        this.mContext = context;
        init();
    }

    public VirtualView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mContext = context;
        init();
    }

    private void init() {
        mVirtualPaint = new Paint();
        surplusHeight = Constant.HEIGHT_OF_SCREEN - Constant.HEIGHT_OF_STATUSBAR
                - Utils.dpToPx(mContext, 64) - Utils.dpToPx(mContext, 85);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // draw背景为白色
        canvas.drawColor(Color.parseColor("#FFFFFF"));
        mVirtualPaint.setAlpha(mAlpha);
        canvas.drawBitmap(mBackgroundBitmap, mBackgroundMatrix, mVirtualPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        mBackgroundMatrix = new Matrix();
        if (mBackgroundBitmap != null) {
            float surplusRate = (float) Constant.WIDTH_OF_SCREEN / (float)surplusHeight;
            float pictureRate = (float)mBackgroundBitmap.getWidth() / (float)mBackgroundBitmap.getHeight();
            if (pictureRate > surplusRate) {
                scaleRate = (float)Constant.WIDTH_OF_SCREEN / (float)mBackgroundBitmap.getWidth();
                setMeasuredDimension(Constant.WIDTH_OF_SCREEN, Constant.WIDTH_OF_SCREEN * mBackgroundBitmap.getHeight() / mBackgroundBitmap.getWidth());
            } else {
                scaleRate = (float)surplusHeight / (float)mBackgroundBitmap.getHeight();
                setMeasuredDimension(surplusHeight * mBackgroundBitmap.getWidth() / mBackgroundBitmap.getHeight(), surplusHeight);
            }
            mBackgroundMatrix.postScale(scaleRate, scaleRate);
        } else {
            setMeasuredDimension(Constant.WIDTH_OF_SCREEN, surplusHeight);
        }
    }

    public void setBitmap(Bitmap bitmap) {
        this.mBackgroundBitmap = bitmap;
        invalidate();
    }

    /**
     * 设置图片的透明度
     */
    public void setImageAlpha(int alpha) {
        this.mAlpha = alpha;
    }

    /**
     *  获取设置透明度后的bitmap
     */
    public Bitmap getmBackgroundBitmap() {
        Paint translatePaint = new Paint();
        translatePaint.setAlpha(mAlpha);
        Bitmap mBackgroundTransBitmp = Bitmap.createBitmap(mBackgroundBitmap.getWidth(), mBackgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas mBackgroundCanvas = new Canvas(mBackgroundTransBitmp);
        mBackgroundCanvas.drawBitmap(mBackgroundBitmap, 0, 0, translatePaint);
        return mBackgroundTransBitmp;
    }
}
